package org.study.security.utils;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @program: securityparent
 * @description:
 * @author: lishuang
 * @create: 2021-12-03 16:45
 **/
@Component
public class TokenUtil {
    /**
     * 有效时长
     */
    private long expiration =24*60*68;

    /**
     * 加密盐值
     * 秘钥
     */
    private String signKey="ls980203";


    /**
     * 根据用户名生成token
     * @param username
     * @return
     */
    public String generateToken(String username){
       return Jwts.builder().setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis()+expiration))
                .signWith(SignatureAlgorithm.HS512, signKey).compressWith(CompressionCodecs.GZIP).compact();
    }

    /**
     * 根据token得到用户信息
     * @param token
     * @return
     */
    public String getUserInfoFromToken(String  token){
        return Jwts.parser().setSigningKey(signKey)
                .parseClaimsJws(token).getBody().getSubject();
    }

    /**
     * 删除token
     * @param token
     */
    public  void removeToken(String token){

    }
}
